/**
 * 
 */
package br.edu.ufcg.embedded.mysocial;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultEdge;
import br.embedded.socialapp.business.Coordenates;
import br.embedded.socialapp.business.ProfileServiceWS;
import br.embedded.socialapp.business.ProfileServiceWSService;
import br.embedded.socialapp.business.Usuario;

/**
 * @author daniel
 *
 */
public class ClientWS
{
	private static ProfileServiceWSService servico;
	private static ProfileServiceWS clientWS;
	
	private static List<Usuario> usersList;
	private static List<List<Coordenates>> listCoordenates;

	private static final int NUMBER_OF_VERTICES = 100;
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		servico = new ProfileServiceWSService();
		clientWS = servico.getProfileServiceWSPort();
		
		usersList = UserGenerator.generateUsers(NUMBER_OF_VERTICES);
		//registerUsers(usersList);

		listCoordenates = CoordinatesGenerator.generateCoordinates(-7.212466615909, -35.908287420042, -7.227505599999965, 
																   -35.89469999999955, 2501.0, -0.00005, 1000);
		while (true)
		{
			int index = 47;

			while (index == 47)
				index = UserGenerator.getUser(usersList);

			Usuario u = usersList.get(index);

			int i = CoordinatesGenerator.getCoordinate(listCoordenates.get(0));
			Coordenates c = listCoordenates.get(0).get(i);
			clientWS.registerLocation(u.getLogin(), c.getLatitude(), c.getLongitude());

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		//profileService.fetchPerson("494929728");
		//profileService.fetchFriends("494929728");
		//profileService.fetchFriendsDetailed("494929728");

		//ProfileService service = new ProfileService("ORKUT_SANDBOX");
		//service.fetchPerson("06185051281244906819");
		//service.fetchFriends("01206547157695522917");
		//service.fetchFriendsDetailed("01206547157695522917");

		/*Person person = port.fetchPerson("05504131713099562528", "ORKUT_SANDBOX");

		System.out.println("User: " + person.getPname().getGivenName());
		System.out.println("About: " + person.getAboutMe());
		System.out.println("Id: " + person.getId());
		System.out.println();*/

		/*		List<User> friends = clientWS.fetchFriends("05504131713099562528", "ORKUT_SANDBOX", true);

		System.out.println("Friends: ");
		System.out.println();

		for (User friend : friends)
		{
			System.out.println("Friend: " + friend.getPname().getGivenName() + " " + friend.getPname().getFamilyName());
			System.out.println(friend.getAboutMe());
			System.out.println(friend.getId());
			System.out.println(friend.getGender());
			System.out.println(friend.getRelationshipStatus());
			System.out.println(friend.getAge());
			System.out.println(friend.getThumbnailUrl());
			System.out.println(friend.getProfileUrl());
			System.out.println(friend.getSmoker());
			System.out.println(friend.getDrinker());
			System.out.println(friend.getChildren());
			System.out.println(friend.getDateOfBirth());
			System.out.println(friend.getEthnicity());
			System.out.println(friend.getNickname());
			System.out.println(friend.getReligion());
			//System.out.println(friend.getA.getCurrentLocation().getCountry());
			System.out.println(friend.getInterests());
			System.out.println();
		}
		 */
		/*		
		//BreadthFirstIterator<Usuario, DefaultEdge> iterator = new BreadthFirstIterator<Usuario, DefaultEdge>(graph);

		DepthFirstIterator<Usuario, DefaultEdge> iterator = 
			new DepthFirstIterator<Usuario, DefaultEdge>(graph);

		iterator.setCrossComponentTraversal(true);
		iterator.addTraversalListener(fullGraph);

		System.out.println("DepthFirst: ");

		while (iterator.hasNext())
			System.out.println(iterator.next().getLogin());

		GraphCache graphCache = new GraphCache(graph);
		graphCache.cacheFriendshipLevels();

		Set<String> vertexes = graphCache.getAllVertex();

		for (String vertex : vertexes) {
			System.out.println(vertex + " - (1): " + graphCache.getNeighboursAtLevel(vertex, 1));
			System.out.println(vertex + " - (2): " + graphCache.getNeighboursAtLevel(vertex, 2));
		}

		System.out.println(graph);
		JGraphSwing.drawGraph(graph);
		 */
	}
	
	private static void registerUsers(List<Usuario> usersList)
	{
		for (int i = 0; i < usersList.size(); i++)
		{
			Usuario u = usersList.get(i);
			clientWS.createUser(u.getLogin(), u.getFirstName(), u.getLastName(), u.getPasswd());
		}
	}
	
	public Coordenates nextCoordinate(List<Coordenates> list, int current)
	{
		if (current < list.size() - 1)
			return list.get(++current);
		
		return null;
	}
	
	public synchronized void registerLocation(Coordenates coordenates)
	{
		clientWS.registerLocation(coordenates.getUserLogin(), coordenates.getLatitude(), coordenates.getLongitude());
	}
	
	public static Graph<Usuario, DefaultEdge> createGraph(int usersCount, int friendsCount)
	{
		List<Usuario> users = UserGenerator.generateUsers(usersCount);
		return UserGenerator.buildFriendshipGraph(users, friendsCount);
	}
	
	private static String dateToString(Date date)
	{
		SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		myFormat.setTimeZone(TimeZone.getTimeZone ("GMT-3"));
		return myFormat.format(date);
	}
}
